package com.gegong.imr.client.fx.controller;

import com.gegong.imr.client.config.ImrClientConfig;
import com.gegong.imr.client.tasks.AbstractProcessCommandTask;
import com.gegong.imr.client.tasks.AbstractReportStatusTask;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@FXMLController
@Slf4j
public class ImrClientMainViewController implements Initializable {

    private final AbstractReportStatusTask reportStatusTask;
    private final AbstractProcessCommandTask processCommandTask;
    private final ImrClientConfig clientConfig;

    @FXML
    private Button controlButton;
    @FXML
    private Button openLogButton;
    @FXML
    private Circle clientStatusCircle;

    @Value("${logging.file}")
    private String logFilePath;

    private ScheduledExecutorService reportService;
    private ScheduledExecutorService processService;

    @Autowired
    public ImrClientMainViewController(AbstractReportStatusTask reportStatusTask, ImrClientConfig clientConfig, AbstractProcessCommandTask processCommandTask) {
        this.reportStatusTask = reportStatusTask;
        this.clientConfig = clientConfig;
        this.processCommandTask = processCommandTask;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    public void onControlButtonAction() {
        Platform.runLater(() -> {
            switch (controlButton.getText()) {
                case "启动":
                    (reportService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("客户端状态上报线程-%d").build())).scheduleWithFixedDelay(
                            reportStatusTask,
                            clientConfig.getClientStatusReportInitDelay(),
                            clientConfig.getClientStatusReportingInterval(),
                            TimeUnit.SECONDS);

                    (processService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("任务执行线程-%d").build())).scheduleWithFixedDelay(
                            processCommandTask,
                            0,
                            clientConfig.getTaskPollingInterval(),
                            TimeUnit.SECONDS);
                    controlButton.setText("关闭");
                    clientStatusCircle.setFill(Color.GREEN);
                    break;
                case "关闭":
                    reportService.shutdown();
                    processService.shutdown();
                    controlButton.setText("启动");
                    clientStatusCircle.setFill(Color.GRAY);
                    break;
            }
        });
    }


    @FXML
    public void onOpenLogButtonAction() {
        //Alert alert = new Alert(Alert.AlertType.INFORMATION, logFilePath);
        //alert.show();
        File logFile = new File(logFilePath);
        //new Alert(Alert.AlertType.INFORMATION, Boolean.toString(logFile.exists())).show();

        try {
            Runtime.getRuntime().exec("notepad " + logFile.getAbsolutePath());
        } catch (IOException e) {
            new Alert(Alert.AlertType.ERROR, e.getMessage());
        }
    }
}
